// RuleJudge.Judge.HEV
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
// using System.Windows.Media.Media3D;
// using Common.Enum;
using System.Windows.Forms;
using RuleJudge.Judge;
using RuleJudge.RuleData;
using SLANCommon.Enum;

public class HEV : BaseJudge
{
    public HEV()
    {
        Type = EProjectType.AQ;
        base.TubesNo = new List<int> { 1, 1, 1, 1, 2, 2, 2 };
        base.ChannelsNo = new List<int> { 1, 2, 3, 4, 1, 2, 3 };
    }

    int FAM = 0;
    int VIC = 1;
    int ROX = 2;
    int CY5 = 3;
    const double EV71_Cutoff = 35.00;
    const double CA16_Cutoff = 35.00;
    const double EV_Cutoff = 35.00;
    const double CA6_Cutoff = 35.00;
    const double CA10_Cutoff = 35.00;
    const double PC_Cutoff = 35.00;
    const double NC_Cutoff = 35.00;
    bool Leak_NC_Control = false;
    bool Leak_PC_Control = false;

    // 开始阴/阳性质控、待测(Unknow)样本、重测(Retest)样本判读
    public override string Judge(List<Sample> samples)
    {
        if (samples.Count == 0)
        {
            return "无判断样本！";
        }

        // 阴性指控判断
        List<Sample> NC_list = samples.Where((Sample s) => s.Type == SampleType.Negative).ToList();
        if (NC_list.Count != 0)
        {
            foreach (Sample NC in NC_list)
            {
                bool NC_Check_Pass = NC.Targets.Where((Target s) => double.IsInfinity(s.Result.Ct) && s.ChannelNo != 3).Count() == NC.Targets.Where((Target s) => s.ChannelNo != 3).Count() &&
                                    NC.Targets.Where((Target s) => s.Result.Ct <= NC_Cutoff && s.ChannelNo == 3).Count() == NC.Targets.Where((Target s) => s.ChannelNo == 3).Count();
                NC.Conclusion.Add(new Conclusion
                {
                    Content = (NC_Check_Pass ? "阴性对照合格" : "阴性对照不合格"),
                    Color = (NC_Check_Pass ? Colors.Green : Colors.Red),
                    BackgroundColor = (NC_Check_Pass ? Colors.Green : Colors.Yellow),
                    ForegroundColor = (NC_Check_Pass ? Colors.Pink : Colors.Blue)
                });
            }
        }
        else
        {
            Leak_NC_Control = true;
        }

        // 阳性质控判读
        List<Sample> PC_list = samples.Where((Sample s) => s.Type == SampleType.HighPositive).ToList();
        if (PC_list.Count != 0)
        {
            foreach (Sample PC in PC_list)
            {
                bool PC_Check_Pass = PC.Targets.Where((Target s) => s.Result.Ct <= PC_Cutoff).Count() == PC.Targets.Count;
                PC.Conclusion.Add(new Conclusion
                {
                    Content = (PC_Check_Pass ? "阳性对照合格" : "阳性对照不合格"),
                    Color = (PC_Check_Pass ? Colors.Green : Colors.Red),
                    BackgroundColor = (PC_Check_Pass ? Colors.Green : Colors.Yellow)
                });
            }
        }
        else
        {
            Leak_PC_Control = true;
        }

        // 反应管AB各通道靶标
        //      0       1       2      3
        //  ________________________________
        //  |  FAM  |  VIC  |  ROX  |  CY5  |
        //  |  EV71 |  CA16 |  IC-A |  EV   |
        //  |-------------------------------|
        //  |  FAM  |   VIC |   ROX |  CY5  |
        //  |  CA6  |  CA10 |  IC-B |   /   |
        //  |-------------------------------|

        // 待测(Unknown)样本判读
        List<Sample> UnknownSample_list = samples.Where((Sample s) => s.Type == SampleType.Unknown).ToList();
        foreach (Sample Unknown_Sample in UnknownSample_list)
        {
            bool IC_Check_Pass = true;
            // 非内参通道的Ct均大于35
            if ((Unknown_Sample.Tubes[0].Targets.Where((Target s) => s.Result.Ct > PC_Cutoff && s.ChannelNo != 3).Count() == Unknown_Sample.Tubes[0].Targets.Where((Target s) => s.ChannelNo != 3).Count() && Unknown_Sample.Tubes[0].Targets[ROX].Result.Ct > PC_Cutoff) ||
                (Unknown_Sample.Tubes[1].Targets.Where((Target s) => s.Result.Ct > PC_Cutoff && s.ChannelNo != 3).Count() == Unknown_Sample.Tubes[1].Targets.Where((Target s) => s.ChannelNo != 3).Count() && Unknown_Sample.Tubes[1].Targets[ROX].Result.Ct > PC_Cutoff))
            {
                IC_Check_Pass = false;
            }
            if (!IC_Check_Pass)
            {
                Unknown_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = "检测结果无效",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                });
                Unknown_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = "检测结果无效",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                });
            }
            else
            {
                // EV71判读
                bool EV71_Positive = Unknown_Sample.Tubes[0].Targets[FAM].Result.Ct <= EV71_Cutoff;
                Unknown_Sample.Tubes[0].Targets[FAM].Conclusion.Add(new Conclusion
                {
                    Content = EV71_Positive ? "EV71:阳性" : "EV71:阴性",
                    Color = EV71_Positive ? Colors.Red : Colors.Green,
                    BackgroundColor = EV71_Positive ? Colors.LightCoral : Colors.LightGreen,
                });

                // CA16判读
                bool CA16_Positive = Unknown_Sample.Tubes[0].Targets[VIC].Result.Ct <= CA16_Cutoff;
                Unknown_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = CA16_Positive ? "CA16:阳性" : "CA16:阴性",
                    Color = CA16_Positive ? Colors.Red : Colors.Green,
                    BackgroundColor = CA16_Positive ? Colors.LightCoral : Colors.LightGreen,
                });

                // EV(通用型)判读
                bool EV_Positive = Unknown_Sample.Tubes[0].Targets[CY5].Result.Ct <= EV_Cutoff;
                Unknown_Sample.Tubes[0].Targets[CY5].Conclusion.Add(new Conclusion
                {
                    Content = EV_Positive ? "EV(通用型):阳性" : "EV(通用型):阴性",
                    Color = EV_Positive ? Colors.Red : Colors.Green,
                    BackgroundColor = EV_Positive ? Colors.LightCoral : Colors.LightGreen,
                });

                // CA6判读
                bool CA6_Positive = Unknown_Sample.Tubes[1].Targets[FAM].Result.Ct <= CA6_Cutoff;
                Unknown_Sample.Tubes[1].Targets[FAM].Conclusion.Add(new Conclusion
                {
                    Content = CA6_Positive ? "CA6:阳性" : "CA6:阴性",
                    Color = CA6_Positive ? Colors.Red : Colors.Green,
                    BackgroundColor = CA6_Positive ? Colors.LightCoral : Colors.LightGreen,
                });

                // CA10判读
                bool CA10_Positive = Unknown_Sample.Tubes[1].Targets[VIC].Result.Ct <= CA10_Cutoff;
                Unknown_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = CA10_Positive ? "CA10:阳性" : "CA10:阴性",
                    Color = CA10_Positive ? Colors.Red : Colors.Green,
                    BackgroundColor = CA10_Positive ? Colors.LightCoral : Colors.LightGreen,
                });
            }
        }

        // 重测(Retest)样本判读
        List<Sample>RetestSample_list = samples.Where((Sample s) => s.Type == SampleType.RetestSample).ToList();
        foreach (Sample Retest_Sample in RetestSample_list)
        {
            bool IC_Check_Pass = true;
            // 非内参通道的Ct均大于35
            if ((Retest_Sample.Tubes[0].Targets.Where((Target s) => s.Result.Ct > PC_Cutoff && s.ChannelNo != 3).Count() == Retest_Sample.Tubes[0].Targets.Where((Target s) => s.ChannelNo != 3).Count() && Retest_Sample.Tubes[0].Targets[ROX].Result.Ct > PC_Cutoff) ||
                (Retest_Sample.Tubes[1].Targets.Where((Target s) => s.Result.Ct > PC_Cutoff && s.ChannelNo != 3).Count() == Retest_Sample.Tubes[1].Targets.Where((Target s) => s.ChannelNo != 3).Count() && Retest_Sample.Tubes[1].Targets[ROX].Result.Ct > PC_Cutoff))
            {
                IC_Check_Pass = false;
            }
            if (!IC_Check_Pass)
            {
                Retest_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = "检测结果无效",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                });
                Retest_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = "检测结果无效",
                    Color = Colors.Blue,
                    BackgroundColor = Colors.LightYellow,
                });
            }
            else
            {
                // EV71判读
                bool EV71_Positive = Retest_Sample.Tubes[0].Targets[FAM].Result.Ct <= EV71_Cutoff;
                Retest_Sample.Tubes[0].Targets[FAM].Conclusion.Add(new Conclusion
                {
                    Content = EV71_Positive ? "EV71:阳性" : "EV71:阴性",
                    Color = EV71_Positive ? Colors.Red : Colors.Green,
                    BackgroundColor = EV71_Positive ? Colors.LightCoral : Colors.LightGreen,
                });

                // CA16判读
                bool CA16_Positive = Retest_Sample.Tubes[0].Targets[VIC].Result.Ct <= CA16_Cutoff;
                Retest_Sample.Tubes[0].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = CA16_Positive ? "CA16:阳性" : "CA16:阴性",
                    Color = CA16_Positive ? Colors.Red : Colors.Green,
                    BackgroundColor = CA16_Positive ? Colors.LightCoral : Colors.LightGreen,
                });

                // EV(通用型)判读
                bool EV_Positive = Retest_Sample.Tubes[0].Targets[CY5].Result.Ct <= EV_Cutoff;
                Retest_Sample.Tubes[0].Targets[CY5].Conclusion.Add(new Conclusion
                {
                    Content = EV_Positive ? "EV(通用型):阳性" : "EV(通用型):阴性",
                    Color = EV_Positive ? Colors.Red : Colors.Green,
                    BackgroundColor = EV_Positive ? Colors.LightCoral : Colors.LightGreen,
                });

                // CA6判读
                bool CA6_Positive = Retest_Sample.Tubes[1].Targets[FAM].Result.Ct <= CA6_Cutoff;
                Retest_Sample.Tubes[1].Targets[FAM].Conclusion.Add(new Conclusion
                {
                    Content = CA6_Positive ? "CA6:阳性" : "CA6:阴性",
                    Color = CA6_Positive ? Colors.Red : Colors.Green,
                    BackgroundColor = CA6_Positive ? Colors.LightCoral : Colors.LightGreen,
                });

                // CA10判读
                bool CA10_Positive = Retest_Sample.Tubes[1].Targets[VIC].Result.Ct <= CA10_Cutoff;
                Retest_Sample.Tubes[1].Targets[VIC].Conclusion.Add(new Conclusion
                {
                    Content = CA10_Positive ? "CA10:阳性" : "CA10:阴性",
                    Color = CA10_Positive ? Colors.Red : Colors.Green,
                    BackgroundColor = CA10_Positive ? Colors.LightCoral : Colors.LightGreen,
                });
            }
        }

        // 质控异常或缺失提示
        if (Leak_NC_Control || Leak_PC_Control)
        {
            MessageBox.Show("实验缺少阴性质控或阳性质控！是否忽略？", "质控提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        return string.Empty;
    }
}